let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: null,
    // 刷新加载
    loadingMoreHidden:true,
    page:1,
    limit:10,
    totalPages:1,
    hideHeader: true,
    hideBottom: true,
    centerList:[],
    isPickMoney:false,
    balance:0,
    changeIndex:0,//选择的tab页
    waitGrantCommission:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    _this.setData({
      user: app.getUser(),
      balance:app.getUser().credit2,
    });
    _this.selectComponent('#cash-withdrawal').getBankList()
    this.getAllUserInfo();
  },
  //获取用户信息，积分字段
  getAllUserInfo() {
    let _this = this;
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/member',
      data: {
        id: _this.data.user.id,
      },
      load: 1
    }).then((res) => {
      res.data.data.credit2 = res.data.data.credit2.toFixed(2);
      _this.data.user = res.data.data;
      wx.setStorageSync('userInfo', JSON.stringify(_this.data.user))
      _this.setData({
        user: _this.data.user,
      });
      _this.getMerchantList();
    })
  },
  //加载
  onReachBottom: function() {
    let that = this;
    that.setData({
      page: that.data.page + 1,
    });
    if(that.data.page < that.data.totalPages || that.data.page == that.data.totalPages){
      this.getMerchantList();
      that.setData({
        hideBottom: false
      });
    }
    setTimeout(function () {
      that.setData({
        hideBottom: true
      })
    }, 1000);
  },
  //刷新
  onPullDownRefresh: function() {
    let that = this;
    that.setData({
      page: 1
    });
    that.setData({
      hideHeader: false,
      loadingMoreHidden: true,
    })
    setTimeout(function () {
      var date = new Date();
      that.setData({
        page: 1,
        hideHeader: true
      })
      that.getMerchantList()
    }, 1000);
    wx.stopPullDownRefresh()
  },
  //获取商户列表
  getMerchantList:function(){
    let that = this;
    if(that.data.changeIndex == 0){
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/commission/log',
        data: {
          offset:(that.data.page -1)*that.data.limit,
          limit:that.data.limit,
          type:'',
          mid:that.data.user.id
        },
      }).then(res =>{
        var tempArray = that.data.centerList;
        that.setData({
          "centerList":res.data.data.rows,
          'totalPages':res.data.data.total,
        });
        let centerList = [];
        if (that.data.page == 1) {
          that.setData({
            'centerList': res.data.data.rows,
          })
        } else {
          tempArray = tempArray.concat(res.data.data.rows);
          that.setData({
            loadingMoreHidden: true,
            centerList: tempArray,
          });
        };
        tempArray = [];
        if(res.data.data.rows.length != that.data.limit){
          that.setData({
            loadingMoreHidden: false,
          });
        }
      })
    }else{
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/mem/getMemByUserId',
        data: {
          offset:(that.data.page-1)*that.data.limit,
          limit:that.data.limit,
          mid:that.data.user.id
        },
      }).then(res =>{
        var tempArray = that.data.centerList;
        that.setData({
          "centerList":res.data.data,
          'totalPages':res.data.data.total,
        });
        let centerList = [];
        if (that.data.page == 1) {
          that.setData({
            'centerList': res.data.data,
          })
        } else {
          tempArray = tempArray.concat(res.data.data);
          that.setData({
            loadingMoreHidden: true,
            centerList: tempArray,
          });
        };
        tempArray = [];
        if(res.data.data.length != that.data.limit){
          that.setData({
            loadingMoreHidden: false,
          });
        }
      })
    }
  },  
  //接收提现组件传值，控制弹框
  isPickMoneyCollect(event){
    this.setData({
      isPickMoney: event.detail
    })
  },
  //接收提现组件传值，改变余额
  userCollect(event){
    this.setData({
      user: event.detail
    })
  },
  //提现打开弹框
  pickMonitor(){
    this.setData({
      isPickMoney: true
    })
  },
  tabChange:function(e){
    console.log(e);
    let that = this;
    that.setData({
      changeIndex:e.detail.index,
      page:1,
    });
    that.getMerchantList();
  },
})